{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "请求体是客户端发送给 API 的数据。响应体是 API 发送给客户端的数据。\n",
    "\n",
    "API 基本上肯定要发送响应体，但是客户端不一定发送请求体。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [26116]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:57326 - \"GET /docs HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57326 - \"GET /openapi.json HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57327 - \"POST /items/ HTTP/1.1\" 422 Unprocessable Entity\n",
      "INFO:     127.0.0.1:57360 - \"POST /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:57434 - \"POST /items/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [26116]\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel\n",
    "from typing import Union\n",
    "import uvicorn\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: Union[str, None] = None\n",
    "    price: float\n",
    "    tax: Union[float, None] = None\n",
    "\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.post(\"/items/\")\n",
    "async def create_item(item: Item):\n",
    "    return item\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 请求体 + 路径参数 + 查询参数\n",
    "* 路径中声明了相同参数的参数，是路径参数\n",
    "* 类型是（int、float、str、bool 等）单类型的参数，是查询参数\n",
    "* 类型是 Pydantic 模型的参数，是请求体"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [26116]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:57533 - \"POST /items/123456?q=good HTTP/1.1\" 200 OK\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel\n",
    "from typing import Union\n",
    "import uvicorn\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: Union[str, None] = None\n",
    "    price: float\n",
    "    tax: Union[float, None] = None\n",
    "\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def create_item(item_id: int, item: Item, q: str | None = None):\n",
    "    result = {'item_id': item_id, **item.dict()}\n",
    "    if q:\n",
    "       result.update({\"q\": q})\n",
    "    return result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}